// PDMSPerObjManager.cpp: implementation of the PDMSPerObjManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PDMSPerObjManager.h"
#include "Tree.h"
#include "Hash.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace PDMS
{
	namespace PDMSFrame
	{
		PDMSPerObjManager::PDMSPerObjManager()
		{
			m_hash.Database( this );
			m_lEnvID = 1;
			m_lMaxElementID = 1;
			m_pIDGen = NULL;
		}
		
		PDMSPerObjManager::~PDMSPerObjManager()
		{
			if (m_pIDGen != NULL)
				delete m_pIDGen;			
		}
		
		UeObjectId PDMSPerObjManager::GenerateId()
		{
			if ( m_pIDGen == NULL )
				m_pIDGen = new IDGenerator( m_lEnvID, m_lMaxElementID );
			return m_pIDGen->Generate();
		}
		
		UESoft::ErrorStatus PDMSPerObjManager::TreeModel( Tree*& pTree, UeDb::OpenModel om )
		{
			Tree* ptmpTree = NULL;
			if ( m_WorldId.IsNull() ) 
			{
				ptmpTree = PDMSClassRegister::Instance()
					->CreateObject( UE_PDMSCLASS_REGKEY( World ) );
				ASSERT( ptmpTree );
				m_hash.Add( ptmpTree );
				m_WorldId = ptmpTree->Id();
				ptmpTree->HashModel( &m_hash );
				ptmpTree->Close();
			}
			UESoft::ErrorStatus es = m_hash.GetObject( m_WorldId, pTree );

			return es;
		}
		
		UESoft::ErrorStatus PDMSPerObjManager::GetObject(const UeObjectId& id, 
					UeDbObject*& pTree, UeDb::OpenModel om /* = UeDb::eForWrite  */)
		{
			return m_hash.GetObject( id, pTree, om );
		}
		
		PDMSPerObjManager::IteratorPtr PDMSPerObjManager::GetObject(const StdString& id )
		{
			return m_hash.GetObject( id );
		}

		Hash* PDMSPerObjManager::HashModel()
		{
			return &m_hash;
		}
		
		void PDMSPerObjManager::RemoveAll()
		{
			HashModel()->RemoveAll();
		}

		UESoft::ErrorStatus PDMSPerObjManager::Save()
		{
			ASSERT( m_PFPtr != NULL );

			UESoft::ErrorStatus es = m_PFPtr->Open();
			if ( es != UESoft::eOk )
				return es;

			es = m_PFPtr->SaveDBInfo( this );
			if ( es != UESoft::eOk )
				return es;

			Hash::IteratorPtr hashPtr = m_hash.Iterator( TRUE, FALSE );
			for ( hashPtr->Begin(); !hashPtr->End(); hashPtr->Next() )
			{
				UeDbObject* pObj = NULL;
				es = hashPtr->GetObject( pObj, UeDb::eForWrite );
				if ( es != UESoft::eOk )
				{
					ASSERT( FALSE );
					m_PFPtr->Close();
					return es;
				}
				es = m_PFPtr->Save( pObj );
				if ( es != UESoft::eOk )
				{
					ASSERT( FALSE );
					pObj->Close();
					m_PFPtr->Close();
					return es;
				}
				pObj->Close();
			}
			es = m_PFPtr->Close();
			if ( es != UESoft::eOk )
				return es;
			
			return UESoft::eOk;
		}
		
		UESoft::ErrorStatus PDMSPerObjManager::Create()
		{
			ASSERT( m_PFPtr == NULL );

			UESoft::ErrorStatus es = PersisterFacade::CreateInstance( 
									 GET_PERSISTER_FACADE_ID( eDESIGN, VersionOne ),
									 Name(), m_PFPtr );
			if ( es != UESoft::eOk )
				return es;

			es = m_PFPtr->Open();			
			if ( es != UESoft::eOk )
				return es;

			es = m_PFPtr->ReadDBInfo( this );
			if ( es != UESoft::eOk )
				return es;

			es = m_PFPtr->Close();
			if ( es != UESoft::eOk )
				return es;

			return UESoft::eOk;
		}
	}
}
